#include  "lp_global_vars.h"
#include	"config_parser.h"

void compute_performance(int sd, short event, void* arg) {
	UNUSED_ARG(sd);
	UNUSED_ARG(event);
	UNUSED_ARG(arg);

	gettimeofday(&performance_end_time, NULL);
	gettimeofday(&endTime_Total, NULL);

	lp_interval_performance->interval_time = measure_time_difference(performance_end_time, performance_start_time);
	lp_interval_performance->total_time = measure_time_difference(endTime_Total, startTime_Total);
	lp_interval_performance->interval_delivery_throughput = calculate_throughput((sizeof(proposer_msg) - PROPOSER_MSG_HEADER) * lp_interval_performance->interval_num_packets_delivered, lp_interval_performance->interval_time);
	lp_interval_performance->interval_sending_throughput = calculate_throughput(lp_interval_performance->interval_bytes_forwarded, lp_interval_performance->interval_time);
	lp_interval_performance->interval_prop_throughput = calculate_throughput(lp_interval_performance->interval_bytes_proposed, lp_interval_performance->interval_time);
	lp_interval_performance->interval_recv_throughput = calculate_throughput(lp_interval_performance->interval_bytes_received, lp_interval_performance->interval_time);

	if (lp_interval_performance->num_packets_delivered_latency == 0)
		lp_interval_performance->interval_latency = 0;
	else
		lp_interval_performance->interval_latency = ((lp_interval_performance->interval_latency) / (double) lp_interval_performance->num_packets_delivered_latency);
	lp_interval_performance->total_num_packets_delivered += lp_interval_performance->interval_num_packets_delivered;

	lp_calculate_performance_over_all_interval_uptonow();
	lp_store_performance_in_array(&lp_performances[0], &lp_next_performance_index, lp_interval_performance);
	lp_print_performance(lp_interval_performance);
	lp_nulify_performance(lp_interval_performance);

	if (lp_next_performance_index >= STOPPING_CONDITION) {
		lp_store_performance_in_file(lp_performance_file, lp_latency_file, lp_sd_avg_file, lp_next_performance_index);
		free(data);
		int i;
		for (i = 0; i < get_config_info()->total_num_proposers; i++)
			free(p_msgs[i]);

		exit(1);
	}
	gettimeofday(&performance_start_time, NULL);
	int status = evtimer_add(print_stat_timeout_eve, &tv1);
	assert(status >= 0);
}

double measure_time_difference(struct timeval now, struct timeval past) {
	struct timeval elapsed_interval;

	elapsed_interval.tv_sec = now.tv_sec - past.tv_sec;
	elapsed_interval.tv_usec = (now.tv_usec > past.tv_usec ? now.tv_usec - past.tv_usec : past.tv_usec - now.tv_usec);
	double msec_interval = 1000 * elapsed_interval.tv_sec + elapsed_interval.tv_usec / (double) 1000;

	return msec_interval;
}

double calculate_throughput(int bytes, double elapsed_time) {
	double thr = (bytes / (double) (1024 * 1024));
	thr = thr / (double) elapsed_time;
	thr = thr * 1000 * 8;
	return thr;
}

void lp_calculate_performance_over_all_interval_uptonow() {
	int i;
	double avg_t = 0, avg_l = 0, avg_s_t = 0, avg_e = 0;
	for (i = 0; i < lp_next_performance_index; i++) {
		avg_s_t += lp_performances[i].interval_sending_throughput;
		avg_t += lp_performances[i].interval_delivery_throughput;
		avg_l += lp_performances[i].interval_latency;
		avg_e += lp_performances[i].interval_num_entrance;
	}
	lp_interval_performance->total_sending_throughput = (lp_interval_performance->interval_sending_throughput + avg_s_t) / (double) (lp_next_performance_index + 1);
	lp_interval_performance->total_delivery_throughput = (lp_interval_performance->interval_delivery_throughput + avg_t) / (double) (lp_next_performance_index + 1);
	lp_interval_performance->total_latency = (lp_interval_performance->interval_latency + avg_l) / (double) (lp_next_performance_index + 1);
	lp_interval_performance->total_num_entrance = (lp_interval_performance->interval_num_entrance + avg_e) / (double) (lp_next_performance_index + 1);
}

void lp_print_performance(lp_performance *p) {
	printf("------------------------------------------------------------------------------------------------------------------\n");
	printf("TOTAL_TIME:   %f   INTER_TIME: %f   TICKS_PER_SECOND: %f\n\n", p->total_time, p->interval_time, p->interval_num_entrance);
	printf("INTERVAL PROP: 	   THROUGHPUT: %f	Bytes: %f\n", p->interval_prop_throughput, p->interval_bytes_proposed);
	printf("INTERVAL SENT: 	   THROUGHPUT: %f	Bytes: %f\n", p->interval_sending_throughput, p->interval_bytes_forwarded);
	printf("INTERVAL RECV: 	   THROUGHPUT: %f	Bytes: %f\n", p->interval_recv_throughput, p->interval_bytes_received);
	printf("INTERVAL DELI: 	   THROUGHPUT: %f	NUM_PACKETS: %d	Own_NUM_PACKETS: %d\n\n", p->interval_delivery_throughput, p->interval_num_packets_delivered, p->num_packets_delivered_latency);
	printf("LATENCY: %f 	PROP_PER_WAKE: %d	\n", p->interval_latency, SUBMIT_PER_WAKE);
	printf("PROPOSER: %d	self_hd:%d	total_hd: %d 	\n", p->next_location_index_in_proposer, p->highest_self_deliverable, p->highest_deliverable_inst_id);
	printf("LD: %d\n", lowestDelivered);
	printf("Num interrupts: %d	(%d)\n", p->num_interrupts, p->num_interrupts_new_msg);
	//	printf("TOTAL DELIVERY:    NUM_PACKETS: %d, THROUGHPUT: %f\n", p->total_num_packets_delivered, p->total_delivery_throughput);
	//	printf("TOTAL SENT:        NUM_PACKETS: %d, THROUGHPUT: %f\n\n", p->total_num_packets_proposed, p->total_sending_throughput);
	//	printf("INTERVAL 		   HOLES:       %d, TOTAL HOLES %d\n\n", p->total_num_holes, p->interval_num_holes);
	//	printf("INTERVAL REDUNDANT PACKETS:     %d  LOST_PACKETS: %d\n", p->interval_num_redundant_packets_received, p->interval_num_packets_lost);
	//	printf("TOTAL    REDUNDANT PACKETS:     %d\n\n", p->total_num_redundant_packets_received);


}

void lp_nulify_performance(lp_performance *p) {

	p->interval_time = 0;
	p->interval_num_packets_delivered = 0;
	p->interval_delivery_throughput = 0;
	p->interval_num_packets_proposed = 0;
	p->interval_sending_throughput = 0;
	p->interval_num_entrance = 0;
	p->interval_latency = 0;
	p->interval_num_packets_received = 0;
	p->interval_bytes_received = 0;
	p->interval_bytes_forwarded = 0;
	p->num_packets_delivered_latency = 0;
	p->interval_prop_throughput = 0;
	p->interval_bytes_proposed = 0;
	p->num_interrupts = 0;
	p->num_interrupts_new_msg = 0;

	if (lp_next_performance_index == 0) {
		p->total_time = 0;
		p->total_num_packets_delivered = 0;
		p->total_delivery_throughput = 0;
		p->total_num_packets_proposed = 0;
		p->total_sending_throughput = 0;
		p->total_num_entrance = 0;
		p->total_latency = 0;
		p->total_num_packets_received = 0;
		p->next_location_index_in_proposer = 0;
		p->highest_deliverable_inst_id = -1;
		p->highest_self_deliverable = -1;
		p->latency_counter = 0;
		p->latency_avg = 0;
		p->latency_sd = 0;
		p->latency_sq_sum = 0;
		p->latency_sum = 0;

	}

}

void lp_store_performance_in_file(const char * performance_file, const char * latency_file, const char * sd_avg_file, int num_entries) {
	FILE *fp;
	int i;
	fp = fopen(performance_file, "a");
	assert(fp != NULL);

	for (i = 0; i < num_entries; i++) {
		//1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19
		fprintf(fp, "%d %d %d %d %f %f %f %f %f %f %f %f %d %f %f\n", i + 1, //1
				SUBMIT_PER_WAKE, //2
				SUBMIT_PER_WAKE, (int) tv2.tv_usec, //3
				lp_performances[i].interval_time, //4
				lp_performances[i].interval_delivery_throughput, //5
				lp_performances[i].interval_sending_throughput, //6
				lp_performances[i].interval_recv_throughput, //7
				lp_performances[i].interval_prop_throughput, //8
				lp_performances[i].interval_bytes_forwarded, //9
				lp_performances[i].interval_bytes_received, //10
				lp_performances[i].interval_bytes_proposed, //11
				lp_performances[i].interval_num_packets_delivered, //12
				lp_performances[i].interval_num_entrance, //13
				lp_performances[i].interval_latency, //14
				lp_performances[i].num_interrupts); //15
	}

	fclose(fp);

	fp = fopen(latency_file, "w");
	assert(fp != NULL);

	for (i = 0; i < lp_interval_performance->latency_counter; i++)
		fprintf(fp, "%f\n", latency[i]);

	fclose(fp);

	lp_interval_performance->latency_avg = lp_interval_performance->latency_sum / lp_interval_performance->latency_counter;
	lp_interval_performance->latency_sd = lp_interval_performance->latency_sq_sum - 2 * lp_interval_performance->latency_avg * lp_interval_performance->latency_sum + lp_interval_performance->latency_counter * pow(lp_interval_performance->latency_avg, 2);
	lp_interval_performance->latency_sd = lp_interval_performance->latency_sd / lp_interval_performance->latency_counter;
	lp_interval_performance->latency_sd = sqrt(lp_interval_performance->latency_sd);

	fp = fopen(sd_avg_file, "w");
	assert(fp != NULL);
	fprintf(fp, "%f %f %d\n", lp_interval_performance->latency_avg, lp_interval_performance->latency_sd, lp_interval_performance->latency_counter);
	fclose(fp);

}

void lp_store_performance_in_array(lp_performance * performances_array, int * performance_index, lp_performance *i_p) {

	memcpy((performances_array + *performance_index), i_p, sizeof(lp_performance));
	*(performance_index) = *(performance_index) + 1;
}

